using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
namespace ICSharpCode.TextEditor
{
	public class BrushRegistry
	{
		private static Dictionary<Color, Brush> brushes = new Dictionary<Color, Brush>();
		private static Dictionary<Color, Pen> pens = new Dictionary<Color, Pen>();
		private static Dictionary<Color, Pen> dotPens = new Dictionary<Color, Pen>();
		private static readonly float[] dotPattern = new float[]
		{
			1f, 
			1f, 
			1f, 
			1f
		};
		public static Brush GetBrush(Color color)
		{
			Dictionary<Color, Brush> obj;
			Monitor.Enter(obj = BrushRegistry.brushes);
			Brush result;
			try
			{
				Brush brush;
				if (!BrushRegistry.brushes.TryGetValue(color, out brush))
				{
					brush = new SolidBrush(color);
					BrushRegistry.brushes.Add(color, brush);
				}
				result = brush;
			}
			finally
			{
				Monitor.Exit(obj);
			}
			return result;
		}
		public static Pen GetPen(Color color)
		{
			Dictionary<Color, Pen> obj;
			Monitor.Enter(obj = BrushRegistry.pens);
			Pen result;
			try
			{
				Pen pen;
				if (!BrushRegistry.pens.TryGetValue(color, out pen))
				{
					pen = new Pen(color);
					BrushRegistry.pens.Add(color, pen);
				}
				result = pen;
			}
			finally
			{
				Monitor.Exit(obj);
			}
			return result;
		}
		public static Pen GetDotPen(Color color)
		{
			Dictionary<Color, Pen> obj;
			Monitor.Enter(obj = BrushRegistry.dotPens);
			Pen result;
			try
			{
				Pen pen;
				if (!BrushRegistry.dotPens.TryGetValue(color, out pen))
				{
					pen = new Pen(color);
					pen.DashPattern = BrushRegistry.dotPattern;
					BrushRegistry.dotPens.Add(color, pen);
				}
				result = pen;
			}
			finally
			{
				Monitor.Exit(obj);
			}
			return result;
		}
	}
}
